package helper

import (
	"fmt"
	"golang-blog/infrastructure/log"

	"github.com/golang-jwt/jwt/v4"
)

// type MyCustomClaims struct {
// 	Foo string `json:"foo"`
// 	jwt.RegisteredClaims
// }

func JwtAuth(tokenString, secrtKey string) error {
	token, err := jwt.ParseWithClaims(
		tokenString,
		&jwt.RegisteredClaims{},
		func(token *jwt.Token) (interface{}, error) {
			return []byte(secrtKey), nil
		})

	claims, ok := token.Claims.(*jwt.RegisteredClaims)
	if !ok || !token.Valid {
		err := fmt.Errorf("validate token fail. err:%v token.Valid:%v", err, token.Valid)
		log.Error(err)
		return err
	}
	log.Debugf("validate token ok:  %v %v", claims.Issuer, claims.ID)
	return nil
}
